import {
  getSwiper,
  getHotSong
} from "../../network/music";
import {
  querySelect
} from "../../utils/query"
// 这个是自己封装的防抖，下面那个是underscore.js包的防抖
// import {
//   throttle
// } from "../../utils/throttle"
import {
  throttle
} from "underscore"
import {
  eventStore
} from "../../store/recommendSong"
import {
  rankStore
} from "../../store/ranking"
import {playStore} from "../../store/player"
const app = getApp()
Page({
  goSearch() {
    wx.navigateTo({
      url: '../music_search/music_search',
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    banners: [], //轮播图数据
    autoHeight: 120, //解决轮播图高度和图片自适应高度不匹配问题导致指示点位置出错，
    recommendSongs: [], //推荐歌曲数据
    hotSong: [], //热榜歌单数据
    screenWidth: 0, //热门歌单的让它到屏幕边缘才会隐藏
    recomendMenu: [], //推荐歌单数据
    // 巅峰榜单
    flag: false,
    topRank: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      screenWidth: app.globalData.screenWidth
    })
    this.ongetSwiper();
    this.ongetHotSong();
    let that = this
    //  this.ongetRecommendSong();
    eventStore.onState("recommendSongs", (value) => {
      // console.log("监听name:", value)
      if (!value.tracks) return
      that.setData({
        recommendSongs: value.tracks.slice(0, 6)
      })
    });
    eventStore.dispatch("fecthSong")
    rankStore.dispatch("fecthRank")
    rankStore.onState("newRanking", this.handnewRank)
    rankStore.onState("originRanking", this.handoriginRanking)
    rankStore.onState("upRanking", this.handupRanking)
  },
  // 获取轮播图数据
  async ongetSwiper() {
    const data = await getSwiper();
    // console.log(data);
    this.setData({
      banners: data.banners
    })
  },
  //解决图片和轮播图高度不一致问题，获取图片高度给轮播图,我们是让getHeight防抖执行
  // 这里只是写法不一样，因为防抖返回一个函数，getHeight:function()
  getHeight: throttle(function () {
    querySelect(".bannerImage").then(val => {
      // console.log(val);
      this.setData({
        autoHeight: val
      })
    })
  }, 200, {
    trailing: false
  }),
  // 获取歌曲信息，改用store实现
  // async ongetRecommendSong() {
  //   //     const data=await getRecommendSong(3778678);
  //   //     console.log(data);
  //   //  let recommendSongs=data.playlist.tracks.slice(0,6);
  //   //  this.setData({
  //   //    recommendSongs
  //   //  })
  // },
  // 点击更多跳转
  onmoreClick() {
    wx.navigateTo({
      url: '../detail_song/detail_song?type=recommendSongs',
    })
  },
  // 获取播放歌单列表
  getSongList(e) {
    const index=e.currentTarget.dataset.index;
    // console.log(index);
    // console.log(this.data.recommendSongs);
    playStore.setState("playSongList",this.data.recommendSongs);
    playStore.setState("playIndex",index)
  },
  // 获取热门歌单数据
  async ongetHotSong() {
    const {
      playlists
    } = await getHotSong("古风");
    this.setData({
      hotSong: playlists
    });
    let data = await getHotSong("华语");
    this.setData({
      recomendMenu: data.playlists
    })
  },
  // 巅峰榜单函数处理,从store获取
  handnewRank(value) {
    if (!value.name) return
    let all = {
      ...this.data.topRank,
      newRanking: value
    };
    this.setData({
      topRank: all
    })
    this.setData({
      flag: true
    })
  },
  handoriginRanking(value) {
    if (!value.name) return
    let all = {
      ...this.data.topRank,
      originRanking: value
    };
    this.setData({
      topRank: all
    })
    this.setData({
      flag: true
    })
  },
  handupRanking(value) {
    if (!value.name) return
    let all = {
      ...this.data.topRank,
      upRanking: value
    };
    this.setData({
      topRank: all
    })
    this.setData({
      flag: true
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    rankStore.offState("newRanking", this.handnewRank)
    rankStore.offState("originRanking", this.handoriginRanking)
    rankStore.offState("upRanking", this.handupRanking)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})